package com.sixbro.shiro.uaa.service;

import com.sixbro.shiro.uaa.model.Role;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * <p>
 *
 * </p>
 *
 * @Author: Mr.Lu
 * @Since: 2020/6/19 17:42
 */
public class RoleServiceImpl implements RoleService {

    private static final Logger LOGGER = LoggerFactory.getLogger(RoleServiceImpl.class);

    private AtomicLong id = new AtomicLong(1);

    private Map<Long, Role> db = new ConcurrentHashMap<>(16);

    @Override
    public Role getById(Long id) {
        return db.get(id);
    }

    @Override
    public Role getByName(String name) {
        for (Role value : db.values()) {
            if (value.getName().equals(name)) {
                return value;
            }
        }
        return null;
    }

    public void addRole(Role role) {
        role.setId(id.getAndIncrement());
        db.put(role.getId(), role);
        LOGGER.info("UAA SERVER INFO : add role [{}] perm-codes: {} to memory", role.getName(), role.getPermCodes());
    }
}
